package com.gz3;

import java.io.*;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;

public class server {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(9999);
        System.out.println("服务器在9999号端口监听，等待连接");
        Socket socket = serverSocket.accept();

        String filepath="src\\picture.gif";
        FileOutputStream fos = new FileOutputStream(filepath);
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        InputStream inputStream = socket.getInputStream();
        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = inputStream.read(buffer)) != -1) {
            fos.write(buffer, 0, len);
        }

        //向客户端发送“收到图片"
        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        bufferedWriter.write("收到图片");
//        bufferedWriter.newLine();
        bufferedWriter.flush();
        socket.shutdownOutput();

        //关闭
        bufferedWriter.close();
        bos.close();
        inputStream.close();
        serverSocket.close();
        socket.close();

    }
}
